package com.study.notepad.dao;/*
 @Author Cedar
 @DESCRIPTION 
*/

import com.study.notepad.bean.User;
import com.study.notepad.util.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDao {

    public User queryById(int userId) {
        User user = null;
        Connection connectionPool = DBUtils.getConnectionPool();
        String sql = "select id, name, role, password, head_img,salt from user where id=?";
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            statement = connectionPool.prepareStatement(sql);
            statement.setInt(1, userId);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                int id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                Integer role = resultSet.getInt(3);
                String password = resultSet.getString(4);
                String headImg = resultSet.getString(5);
                String salt = resultSet.getString(6);
                user = new User();
                user.setId(id);
                user.setName(name);
                user.setRole(role);
                user.setPassword(password);
                user.setHeadImg(headImg);
                user.setSalt(salt);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtils.closeConnection(connectionPool, resultSet, statement);
        }
        return user;
    }

    public User queryByUserName(String userName) {
        User user = null;
        Connection connectionPool = DBUtils.getConnectionPool();
        String sql = "select id, name, role, password, salt, head_img from user where name=?";
        PreparedStatement ps = null;
        ResultSet resultSet = null;
        try {
            ps = connectionPool.prepareStatement(sql);
            ps.setString(1, userName);
            resultSet = ps.executeQuery();
            while (resultSet.next()) {
                int id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                Integer role = resultSet.getInt(3);
                String password = resultSet.getString(4);
                String salt = resultSet.getString(5);
                String headImg = resultSet.getString(6);
                user = new User();
                user.setId(id);
                user.setName(name);
                user.setRole(role);
                user.setPassword(password);
                user.setSalt(salt);
                user.setHeadImg(headImg);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtils.closeConnection(connectionPool, resultSet, ps);
        }
        return user;
    }

    public int insert(String userName, String password, String headImg, String salt) {
        int i = 0;
        Connection connectionPool = DBUtils.getConnectionPool();
        String sql = "insert into user(name, role, password, salt,head_img) values (?,?,?,?,?)";
        PreparedStatement ps = null;
        try {
            ps = connectionPool.prepareStatement(sql);
            ps.setString(1, userName);
            ps.setInt(2, 0);
            ps.setString(3, password);
            ps.setString(4, salt);
            ps.setString(5, headImg);

            i = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtils.closeConnection(connectionPool, null, ps);
        }

        return i;
    }


    public int updateById(int id, String userName, String password, String headImg) {
        int i = 0;
        Connection connectionPool = DBUtils.getConnectionPool();
        String sql = "update user set name = ?, password= ?, head_img = ? where id = ?";
        PreparedStatement ps = null;
        try {
            ps = connectionPool.prepareStatement(sql);
            ps.setString(1, userName);
            ps.setString(2, password);
            ps.setString(3, headImg);
            ps.setInt(4, id);
            i = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtils.closeConnection(connectionPool, null, ps);
        }
        return i;
    }
}
